package com.colin.app.activity.login;

import android.Manifest;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.colin.app.R;
import com.colin.app.app.AppActivity;
import com.colin.app.app.BasePresenterImpl;
import com.colin.library.help.NetHelp;
import com.colin.library.help.StringHelp;

/**
 * Created by Colin on 2017/2/7.
 */

public class LoginPresenterImpl extends BasePresenterImpl implements LoginContract.LoginPresenter {

    private LoginContract.LoginView mLoginView;
    private LoginContract.LoginModel mLoginModel;

    public LoginPresenterImpl(AppActivity mAppActivity, LoginContract.LoginView mLoginView) {
        super(mAppActivity, mLoginView);
        this.mLoginView = mLoginView;
        this.mLoginModel = new LoginModelImpl();
    }

    @Override
    public void setData() {
        mLoginView.setTitle(mAppActivity.getString(R.string.login));
        //默认获取账号密码
        if (mLoginView.checkPermission(Manifest.permission.READ_CONTACTS)){//检查权限
            mLoginView.initLoginUser(mLoginModel.getUser(mAppActivity),mLoginModel.getPassword(mAppActivity));
        }else {
            mLoginView.toastShow("请选择相应权限");
        }

    }


    @Override
    public void getHead() {
        if (mLoginView.checkPermission(Manifest.permission.CAMERA)){
            mLoginView.getImageHeadFromPhone();
        }else {
            mLoginView.toastShow("请选择相应权限");
        }
    }

    @Override
    public void showDeleteImageView(String text, ImageView imageview) {
        imageview.setVisibility(StringHelp.isEmpty(text)? View.INVISIBLE:View.VISIBLE);
    }

    @Override
    public void clearText(EditText editText) {
        mLoginView.hideKeyword();
        editText.setText("");
    }

    @Override
    public void showPassword(ImageView imageView, EditText edit_login_password) {
        mLoginView.hideKeyword();
        boolean isSelected = imageView.isSelected();
        if (isSelected) {
            edit_login_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else if (!isSelected) {
            edit_login_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
        imageView.setSelected(!isSelected);
    }

    @Override
    public void login(EditText edit_login_user, EditText edit_login_password) {
        mLoginView.hideKeyword();
        //检查网络
        if (!NetHelp.isNetworkAvailable(mAppActivity)) {
            mBaseView.toastShow(R.string.net_is_error);
            return;
        }
        if (mLoginView.isLoading()) {
            mLoginView.toastShow(R.string.http_is_loading);
            return;
        }
        //用户账户数据格式
        String user = StringHelp.getText(edit_login_user);
        String password = StringHelp.getText(edit_login_password);
        if (StringHelp.isEmpty(user)) {
            edit_login_user.setError(mAppActivity.getString(R.string.prompt_user));
            edit_login_user.requestFocus();
            return;
        }
        if (StringHelp.isEmpty(password)) {
            edit_login_password.setError(mAppActivity.getString(R.string.prompt_password));
            edit_login_password.requestFocus();
            return;
        }
        //网络请求
//        mLoginModel.login(mAppActivity, Url.BAIDU_URL,user,password,);
    }

}
